﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using BUS;
using DTO;

namespace WindowsFormsApplication1
{
    public partial class SP : Form
    {
        public SP()
        {
            InitializeComponent();
        }
        public class sqlConnectionData
        {
            public static SqlConnection Ketnoi()
            {
                SqlConnection cnn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=Bakery;Integrated Security=True");
                return cnn;
            }
        }
        int i=0;
        public void khoadieukhien()
        {
            txtMaSP.Enabled = false;
            txtTenSP.Enabled=false;
            txtGia.Enabled=false;
            cbLoai.Enabled=false;
            cbSX.Enabled=false;
            txtsl.Enabled=false;

            btnSua.Enabled=true;
            btnThem.Enabled=true;
            btnXoa.Enabled=true;
            btnHuy.Enabled=false;
            btnLuu.Enabled=false;
        }
        public void modieukhien()
    {
        txtMaSP.Enabled=true;
            txtTenSP.Enabled=true;
            txtGia.Enabled=true;
            cbLoai.Enabled=true;
            cbSX.Enabled=true;
            txtsl.Enabled=true;

            btnSua.Enabled=false;
            btnXoa.Enabled=false;
            btnLuu.Enabled=true;
    }
        public void setnull()
        {
            this.txtMaSP.Clear();
            this.txtTenSP.Clear();
            this.txtGia.Clear();
            this.txtsl.Clear();
            this.txtTim.Clear();

        }
        private void SP_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = SanPham_BUS.hienthi();
           SqlConnection cnn = sqlConnectionData.Ketnoi();
           string sql1 = "select * from LOAI";
           SqlDataAdapter ad1 = new SqlDataAdapter(sql1, cnn);
           DataTable dtb = new DataTable();
           ad1.Fill(dtb);
           cbLoai.DataSource = dtb;
           cbLoai.DisplayMember = "TenLoai";
           cbLoai.ValueMember = "MaLoai";
            string sql2="select * from NHASX";
            SqlDataAdapter ad2 = new SqlDataAdapter(sql2,cnn);
            DataTable dtb2 = new DataTable();
            ad2.Fill(dtb2);
            cbSX.DataSource = dtb2;
            cbSX.DisplayMember = "TenNSX";
            cbSX.ValueMember = "MaSX";
            khoadieukhien();

        }
        public bool IsNumber(string pValue)
        {
            foreach (Char c in pValue)
            {
                if (!Char.IsDigit(c))
                    return false;
            }
            return true;
        }
         private void btnThem_Click(object sender, EventArgs e)
        {
             modieukhien();
             btnHuy.Enabled=true;
             btnThem.Enabled=false;
             dataGridView1.Enabled = false;
             i=1;
        }
         private void btnSua_Click(object sender, EventArgs e)
         {
             modieukhien();
             btnHuy.Enabled = true;
             btnThem.Enabled = false;
             dataGridView1.Enabled = false;
             i = 2;
         }
         private void btnLuu_Click(object sender, EventArgs e)
         {

             if (i==1)
             {

                 string soluong = txtsl.Text;
                 if (IsNumber(soluong) == false)
                 {
                     MessageBox.Show

           ("Số lượng phải là số nguyên", "Thông báo");
                     return;
                 }
                 string gia = txtGia.Text;
                 if (IsNumber(gia) == false)
                 {
                     MessageBox.Show("Giá phải là số nguyên", "Thông báo");
                     return;

                 }
                 if (kiemtratrungdl() == true)
                 {
                     MessageBox.Show("Mã bánh không được trùng", "Thông Báo");
                     return;
                 }
                 if (txtMaSP.Text == "" || txtGia.Text == "" || txtsl.Text == "" || txtTenSP.Text == "")
                 {
                     MessageBox.Show("Vui lòng điền đầy đủ thông tin", "Thông Báo");
                     return;
                 }
                 string loai;
                 string nhasx;
                 SqlConnection cnn = sqlConnectionData.Ketnoi();
                 cnn.Open();
                 SqlCommand cmd = new SqlCommand("select * from LOAI where MaLoai='" + cbLoai.SelectedValue.ToString() + "'", cnn);
                 SqlDataReader r = cmd.ExecuteReader();


                 r.Read();
                 loai = r.GetString(0).ToString();
                 r.Close();
                 SqlCommand cmd1 = new SqlCommand("select * from NHASX where MaSX='" + cbSX.SelectedValue.ToString() + "'", cnn);
                 SqlDataReader r1 = cmd1.ExecuteReader();

                 r1.Read();
                 nhasx = r1.GetString(0).ToString();
                 r1.Close();

                 string masp = txtMaSP.Text;
                 string tensp = txtTenSP.Text;

                 SanPham_DTO sp = new SanPham_DTO(masp, tensp, loai, gia, nhasx, soluong);
                 SanPham_BUS.ThemSP(sp);
                 setnull();
                 khoadieukhien();
                 dataGridView1.DataSource = SanPham_BUS.hienthi();
                 MessageBox.Show("Thêm thành công!", "Thông báo");
                 cnn.Close();
             }
             else if(i==2)
             {

                 string old = dataGridView1.CurrentRow.Cells[0].Value.ToString().Trim();
                 string masp = txtMaSP.Text;
                 if (old != masp)
                 {
                     MessageBox.Show("Không được sửa mã bánh!", "Thông Báo");
                     return;
                 }
                 string soluong = txtsl.Text;
                 if (IsNumber(soluong) == false)
                 {
                     MessageBox.Show("Số lượng phải là số nguyên", "Thông báo");
                     return;
                 }
                 string gia = txtGia.Text;
                 if (IsNumber(gia) == false)
                 {
                     MessageBox.Show("Giá phải là số nguyên", "Thông báo");
                     return;

                 }
                 if (txtMaSP.Text == "" || txtGia.Text == "" || txtsl.Text == "" || txtTenSP.Text == "")
                 {
                     MessageBox.Show("Vui lòng điền đầy đủ thông tin", "Thông Báo");
                     return;
                 }
                 string loai;
                 string nhasx;
                 SqlConnection cnn = sqlConnectionData.Ketnoi();
                 cnn.Open();
                 SqlCommand cmd = new SqlCommand("select * from LOAI where MaLoai='" + cbLoai.SelectedValue.ToString() + "'", cnn);
                 SqlDataReader r = cmd.ExecuteReader();


                 r.Read();
                 loai = r.GetString(0).ToString();
                 r.Close();
                 SqlCommand cmd1 = new SqlCommand("select * from NHASX where MaSX='" + cbSX.SelectedValue.ToString() + "'", cnn);
                 SqlDataReader r1 = cmd1.ExecuteReader();

                 r1.Read();
                 nhasx = r1.GetString(0).ToString();
                 r1.Close();

                 string tensp = txtTenSP.Text;
                 SanPham_DTO sp = new SanPham_DTO(masp, tensp, loai, gia, nhasx, soluong);

                 SanPham_BUS.SuaSP(sp, txtMaSP.Text);
                 setnull();
                 khoadieukhien();
                 dataGridView1.DataSource = SanPham_BUS.hienthi();
                 MessageBox.Show("Sửa thành công!", "Thông báo");
                 cnn.Close();
             }
             i = 0;
             dataGridView1.Enabled = true;
             setnull();
         }

        

        private void button2_Click(object sender, EventArgs e)
        {
            TimKiemSP tk = new TimKiemSP();
            tk.Show();
        }


        private void dataGridView1_RowSelected(object sender, EventArgs e)
        {

            if ((i == 0) && (dataGridView1.CurrentRow != null))

            {
                txtMaSP.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
                txtGia.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString().Trim();
                txtsl.Text = dataGridView1.CurrentRow.Cells[5].Value.ToString().Trim();
                txtTenSP.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString().Trim();
                cbLoai.SelectedValue = dataGridView1.CurrentRow.Cells[2].Value.ToString();
                cbSX.SelectedValue = dataGridView1.CurrentRow.Cells[4].Value.ToString();

            }
            else
                dataGridView1.Enabled = true;
        }
        private bool kiemtratrungdl()
        {
            bool tam = false;
            string ma=txtMaSP.Text;
            SqlConnection cnn = sqlConnectionData.Ketnoi();
            cnn.Open();
            string sql = "SELECT * from SANPHAM ";
            SqlCommand cmd = new SqlCommand(sql, cnn);
            cmd.CommandType = CommandType.Text;
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                if (ma == dr.GetString(0))
                {
                    tam = true;
                    break;
                }
            }
            cnn.Close();
            return tam;
        }
        
        
      
        private void button6_Click(object sender, EventArgs e)
        {
            this.txtMaSP.Clear();
            this.txtTenSP.Clear();
            this.txtGia.Clear();
            this.txtsl.Clear();
            this.txtTim.Clear();
            dataGridView1.DataSource = SanPham_BUS.hienthi();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string masp = txtTim.Text;
            SanPham_BUS.timkiem(masp);
            dataGridView1.DataSource = SanPham_BUS.timkiem(masp);
        }
    
 private void txtGia_TextChanged(object sender, EventArgs e)
        {

        
        }

 private void btnXoa_Click(object sender, EventArgs e)
 {
     string masp = dataGridView1.CurrentCell.Value.ToString();
     if (MessageBox.Show("Chắc chắn xóa chứ ?", "Title", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
     {
         SanPham_BUS.XoaSP(masp);
     }
     dataGridView1.DataSource = SanPham_BUS.hienthi();
 }

 private void btnHuy_Click(object sender, EventArgs e)
 {
     setnull();
     khoadieukhien();
     dataGridView1.Enabled = true;
     i = 0;
 }

 
    }
}